package com.lfy.config;

import com.lfy.modules.common.config.sys.DictionariesKey;
import com.lfy.modules.common.config.sys.SysDictionaries;
import com.lfy.modules.common.intercept.AuthHandlerInterceptor;
import com.lfy.modules.safety.filters.BlackFilter;
import com.lfy.modules.safety.filters.SignFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
/**
 *  webmvc配置
 *
 * @author <a href="https://www.lvfeiy.top">lvfeiyang</a>
 * @date 2024/4/25 1:04
 */
@Configuration
@DependsOn(value = {"sysDictionaries"})
public class WebHandlerConfig implements WebMvcConfigurer {
    @Resource
    private AuthHandlerInterceptor authHandlerInterceptor;
    @Resource
    private ParamsConfig paramsConfig;
    /**
     * 前端部署的url
     */
    static String webPath = "http://www.lvfeiy.top";
    @PostConstruct
    public void init() {
        String path = SysDictionaries.getValue(DictionariesKey.SYS_WEB_CORS_URL);
        if(StringUtils.isNotEmpty(path)){
            webPath = path;
        }
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authHandlerInterceptor).addPathPatterns("/admin/**","/statistics/**","/task/**");
        WebMvcConfigurer.super.addInterceptors(registry);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(paramsConfig);
        WebMvcConfigurer.super.addArgumentResolvers(resolvers);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://127.0.0.1:5174","http://127.0.0.1:5173","http://localhost:5173","http://localhost:5174",webPath)
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true);
    }
    @Bean
    public FilterRegistrationBean<BlackFilter> blackFilter() {
        FilterRegistrationBean<BlackFilter> registrationBean = new FilterRegistrationBean<>();

        registrationBean.setFilter(new BlackFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(2);
        return registrationBean;
    }
    @Bean
    public FilterRegistrationBean<SignFilter> signFilter() {
        FilterRegistrationBean<SignFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new SignFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(1);
        return registrationBean;
    }

}
